﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
using System.Xml;
using System.Windows.Forms;

namespace OntologyUtility.Xml
{
    public class UtilXml
    {

       
        
        private void writeAttribute() {

        }

        public static void Itera(Type typell, XDocument document, String tagElement)
        {
            IEnumerable<XElement> elements = document.Root.Descendants(tagElement);
            Console.WriteLine("Type: " + typell.ToString());
            PropertyInfo[] propertyInfo = typell.GetProperties();
            foreach (XElement element in elements)
            {
                for (int i = 0; i < propertyInfo.Length; i++) // p[i] è una property
                {
                    object[] attributes = propertyInfo[i].GetCustomAttributes(true);
                    for (int j = 0; j < attributes.Length; j++)
                    {
                        if (((XmlNodeTypeAttribute)attributes[j]).Type.Equals(XmlNodeType.Element))
                        {
                            String name = propertyInfo[i].ToString().Split(' ')[1];
                            Itera2(propertyInfo[i].ReflectedType.GetProperty(propertyInfo[i].Name).PropertyType, element.Element(name), name);
                        }
                        else if (((XmlNodeTypeAttribute)attributes[j]).Type.Equals(XmlNodeType.Attribute))
                        {
                            try
                            {
                                Console.WriteLine("    " + element.Attribute(propertyInfo[i].Name));
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("ex: " + e.ToString());
                            }
                        }
                    }
                }

            }
        }

        private static void Itera2(Type type, XElement element, String tagElement)
        {
            Console.WriteLine("Type: " + type.ToString());
            IEnumerable<XElement> elements = element.Elements();
            PropertyInfo[] propertyInfo = type.GetProperties();

            foreach (XElement _element in elements)
            {
                for (int i = 0; i < propertyInfo.Length; i++) // p[i] è una property
                {
                    object[] attributes = propertyInfo[i].GetCustomAttributes(true);
                    for (int j = 0; j < attributes.Length; j++)
                    {
                        if (((XmlNodeTypeAttribute)attributes[j]).Type.Equals(XmlNodeType.Element))
                        {
                            String name = propertyInfo[i].ToString().Split(' ')[1];
                            Itera2(propertyInfo[i].ReflectedType.GetProperty(propertyInfo[i].Name).PropertyType, _element.Element(name), name);
                        }
                        else if (((XmlNodeTypeAttribute)attributes[j]).Type.Equals(XmlNodeType.Attribute))
                        {
                            try
                            {
                                Console.WriteLine("    " + _element.Attribute(propertyInfo[i].Name));
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("ex: " + e.ToString());
                            }
                        }
                    }
                }
            }
        }

    }
}
